package com.geeklan.smart.ui.dialog;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.Nullable;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.viewbinding.ViewBinding;

import com.geeklan.smart.R;
import com.geeklan.smart.databinding.DialogBaseBottomSheetBinding;
import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.bottomsheet.BottomSheetDialog;

public abstract class BaseBottomSheetDialog<VB extends ViewBinding> extends BottomSheetDialog {

    protected DialogBaseBottomSheetBinding baseBinding;
    protected VB mBinding;
    protected BottomSheetBehavior<View> behavior;
    protected Context mContext;

    public BaseBottomSheetDialog(Context context) {
        super(context, R.style.BottomSheetDialogTheme);
        mContext = context;
        LayoutInflater layoutInflater = getLayoutInflater();
        baseBinding = DialogBaseBottomSheetBinding.inflate(layoutInflater);
        mBinding = createContentViewBinding(layoutInflater);
        if (mBinding != null) {
            baseBinding.containerContent.addView(mBinding.getRoot());
        }
        setContentView(baseBinding.getRoot());

        FrameLayout bottomSheet = findViewById(com.google.android.material.R.id.design_bottom_sheet);
        if (bottomSheet != null) {
            behavior = BottomSheetBehavior.from(bottomSheet);
            behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
            setDraggable(false);
            bottomSheet.setClipChildren(false);
            bottomSheet.setClipToPadding(false);

            // 递归解除所有父布局的裁剪限制
            ViewGroup parent = (ViewGroup) bottomSheet.getParent();
            while (parent != null) {
                parent.setClipChildren(false);
                parent.setClipToPadding(false);
                parent = (ViewGroup) parent.getParent();
            }
        }
        setNonFullScreen();
        setupDialog();
    }

    public void showBack(boolean isShow) {
        baseBinding.ivBack.setVisibility(isShow ? View.VISIBLE : View.GONE);
    }

    public void setBackOnClickListener(View.OnClickListener onClickListener) {
        baseBinding.ivBack.setOnClickListener(onClickListener);
    }

    public void setContentViewHeight(int height) {
        ViewGroup.LayoutParams layoutParams = baseBinding.containerContent.getLayoutParams();
        layoutParams.height = height;
        baseBinding.containerContent.setLayoutParams(layoutParams);
    }

    public void setDialogTitle(int titleId) {
        baseBinding.tvTitle.setText(titleId);
    }

    public void setDialogTitle(CharSequence title) {
        baseBinding.tvTitle.setText(title);
    }

    /**
     * 是否可以拖动
     *
     * @param draggable
     */
    public void setDraggable(boolean draggable) {
        behavior.setDraggable(draggable);
    }

    protected abstract VB createContentViewBinding(LayoutInflater inflater);

    protected void setupDialog() {
        baseBinding.btnClose.setOnClickListener(v -> dismiss());
    }

    public void setConfirmButtonClickListener(@Nullable View.OnClickListener listener) {
        if (listener != null) {
            baseBinding.btnConfirm.setOnClickListener(listener);
        }
    }

    public void setConfirmButtonVisible(boolean visible) {
        baseBinding.btnConfirm.setVisibility(visible ? View.VISIBLE : View.GONE);
    }

    public void setConfirmButtonText(String text) {
        baseBinding.btnConfirm.setText(text);
    }

    public void setCancelableOutside(boolean cancelable) {
        setCancelable(cancelable);
        setCanceledOnTouchOutside(cancelable);
    }

    /**
     * 设置非全屏高度
     */
    public void setNonFullScreen(float size) {
        // 延迟设置，确保 BottomSheet 的布局已加载完成
        baseBinding.getRoot().post(() -> {
            View bottomSheet = baseBinding.getRoot().getParent() instanceof View ? (View) baseBinding.getRoot().getParent() : null;
            if (bottomSheet != null) {
                CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) bottomSheet.getLayoutParams();
                // 设置最大高度为屏幕高度的 60%
                params.height = (int) (getContext().getResources().getDisplayMetrics().heightPixels * size);
                bottomSheet.setLayoutParams(params);

                // 设置 BottomSheetBehavior 的 peek height
                behavior.setPeekHeight(params.height);
            }
        });
    }

    /**
     * 设置非全屏高度
     */
    public void setNonFullScreen() {
        // 延迟设置，确保 BottomSheet 的布局已加载完成
        baseBinding.getRoot().post(() -> {
            View bottomSheet = baseBinding.getRoot().getParent() instanceof View ? (View) baseBinding.getRoot().getParent() : null;
            if (bottomSheet != null) {
                CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) bottomSheet.getLayoutParams();
                // 设置最大高度为屏幕高度的 60%
                params.height = (int) (getContext().getResources().getDisplayMetrics().heightPixels * 0.6f);
                bottomSheet.setLayoutParams(params);

                // 设置 BottomSheetBehavior 的 peek height
                behavior.setPeekHeight(params.height);
            }
        });
    }
}
